home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / EasyPHP-2.0b1-setup.exe / {app} / phpmyadmin / tbl_alter.php < prev    next >
Encoding:
PHP Script  |  2006-11-18  |  9.3 KB  |  208 lines

  1. <?php
  2. /* $Id: tbl_alter.php 9499 2006-10-04 13:10:13Z nijel $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. /**
  6.  * Gets some core libraries
  7.  */
  8. require_once('./libraries/common.lib.php');
  9. require_once('./libraries/Table.class.php');
  10.  
  11.  
  12. $js_to_run = 'functions.js';
  13. require_once('./libraries/header.inc.php');
  14.  
  15. // Check parameters
  16. PMA_checkParameters(array('db', 'table'));
  17.  
  18. /**
  19.  * Gets tables informations
  20.  */
  21. require_once('./libraries/tbl_properties_common.php');
  22. require_once('./libraries/tbl_properties_table_info.inc.php');
  23. /**
  24.  * Displays top menu links
  25.  */
  26. $active_page = 'tbl_properties_structure.php';
  27. // I don't see the need to display the links here, they will be displayed later
  28. //require('./libraries/tbl_properties_links.inc.php');
  29.  
  30.  
  31. /**
  32.  * Defines the url to return to in case of error in a sql statement
  33.  */
  34. $err_url = 'tbl_properties_structure.php?' . PMA_generate_common_url($db, $table);
  35.  
  36.  
  37. /**
  38.  * Modifications have been submitted -> updates the table
  39.  */
  40. $abort = false;
  41. if (isset($do_save_data)) {
  42.     $field_cnt = count($field_orig);
  43.     for ($i = 0; $i < $field_cnt; $i++) {
  44.         // to """ in tbl_properties.php
  45.         $field_orig[$i]     = urldecode($field_orig[$i]);
  46.         if (strcmp(str_replace('"', '"', $field_orig[$i]), $field_name[$i]) == 0) {
  47.             $field_name[$i] = $field_orig[$i];
  48.         }
  49.         $field_default_orig[$i] = urldecode($field_default_orig[$i]);
  50.         if (strcmp(str_replace('"', '"', $field_default_orig[$i]), $field_default[$i]) == 0) {
  51.             $field_default[$i]  = $field_default_orig[$i];
  52.         }
  53.         $field_length_orig[$i] = urldecode($field_length_orig[$i]);
  54.         if (strcmp(str_replace('"', '"', $field_length_orig[$i]), $field_length[$i]) == 0) {
  55.             $field_length[$i] = $field_length_orig[$i];
  56.         }
  57.         if (!isset($query)) {
  58.             $query = '';
  59.         } else {
  60.             $query .= ', CHANGE ';
  61.         }
  62.  
  63.         $query .= PMA_Table::generateAlter($field_orig[$i], $field_name[$i], $field_type[$i], $field_length[$i], $field_attribute[$i], isset($field_collation[$i]) ? $field_collation[$i] : '', $field_null[$i], $field_default[$i], isset($field_default_current_timestamp[$i]), $field_extra[$i], (isset($field_comments[$i]) ? $field_comments[$i] : ''), $field_default_orig[$i]);
  64.     } // end for
  65.  
  66.     // To allow replication, we first select the db to use and then run queries
  67.     // on this db.
  68.      PMA_DBI_select_db($db) or PMA_mysqlDie(PMA_DBI_getError(), 'USE ' . PMA_backquote($db) . ';', '', $err_url);
  69.     // Optimization fix - 2 May 2001 - Robbat2
  70.     $sql_query = 'ALTER TABLE ' . PMA_backquote($table) . ' CHANGE ' . $query;
  71.     $error_create = FALSE;
  72.     $result    = PMA_DBI_try_query($sql_query) or $error_create = TRUE;
  73.  
  74.     if ($error_create == FALSE) {
  75.         $message   = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
  76.         $btnDrop   = 'Fake';
  77.  
  78.         // garvin: If comments were sent, enable relation stuff
  79.         require_once('./libraries/relation.lib.php');
  80.         require_once('./libraries/transformations.lib.php');
  81.  
  82.         $cfgRelation = PMA_getRelationsParam();
  83.  
  84.         // take care of pmadb internal comments here
  85.         // garvin: Update comment table, if a comment was set.
  86.         if (PMA_MYSQL_INT_VERSION < 40100 && isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
  87.             foreach ($field_comments AS $fieldindex => $fieldcomment) {
  88.                 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
  89.                     PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment, $field_orig[$fieldindex], 'pmadb');
  90.                 }
  91.             }
  92.         }
  93.  
  94.         // garvin: Rename relations&display fields, if altered.
  95.         if (($cfgRelation['displaywork'] || $cfgRelation['relwork']) && isset($field_orig) && is_array($field_orig)) {
  96.             foreach ($field_orig AS $fieldindex => $fieldcontent) {
  97.                 if ($field_name[$fieldindex] != $fieldcontent) {
  98.                     if ($cfgRelation['displaywork']) {
  99.                         $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
  100.                                       . ' SET     display_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
  101.                                       . ' WHERE db_name  = \'' . PMA_sqlAddslashes($db) . '\''
  102.                                       . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\''
  103.                                       . ' AND display_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
  104.                         $tb_rs    = PMA_query_as_cu($table_query);
  105.                         unset($table_query);
  106.                         unset($tb_rs);
  107.                     }
  108.  
  109.                     if ($cfgRelation['relwork']) {
  110.                         $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
  111.                                       . ' SET     master_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
  112.                                       . ' WHERE master_db  = \'' . PMA_sqlAddslashes($db) . '\''
  113.                                       . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
  114.                                       . ' AND master_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
  115.                         $tb_rs    = PMA_query_as_cu($table_query);
  116.                         unset($table_query);
  117.                         unset($tb_rs);
  118.  
  119.                         $table_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
  120.                                       . ' SET     foreign_field = \'' . PMA_sqlAddslashes($field_name[$fieldindex]) . '\''
  121.                                       . ' WHERE foreign_db  = \'' . PMA_sqlAddslashes($db) . '\''
  122.                                       . ' AND foreign_table = \'' . PMA_sqlAddslashes($table) . '\''
  123.                                       . ' AND foreign_field = \'' . PMA_sqlAddslashes($fieldcontent) . '\'';
  124.                         $tb_rs    = PMA_query_as_cu($table_query);
  125.                         unset($table_query);
  126.                         unset($tb_rs);
  127.                     } // end if relwork
  128.                 } // end if fieldname has changed
  129.             } // end while check fieldnames
  130.         } // end if relations/display has to be changed
  131.  
  132.         // garvin: Update comment table for mime types [MIME]
  133.         if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
  134.             foreach ($field_mimetype AS $fieldindex => $mimetype) {
  135.                 if (isset($field_name[$fieldindex]) && strlen($field_name[$fieldindex])) {
  136.                     PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
  137.                 }
  138.             }
  139.         }
  140.  
  141.         $active_page = 'tbl_properties_structure.php';
  142.         require('./tbl_properties_structure.php');
  143.     } else {
  144.         PMA_mysqlDie('', '', '', $err_url, FALSE);
  145.         // garvin: An error happened while inserting/updating a table definition.
  146.         // to prevent total loss of that data, we embed the form once again.
  147.         // The variable $regenerate will be used to restore data in libraries/tbl_properties.inc.php
  148.         if (isset($orig_field)) {
  149.                 $field = $orig_field;
  150.         }
  151.  
  152.         $regenerate = true;
  153.     }
  154. }
  155.  
  156. /**
  157.  * No modifications yet required -> displays the table fields
  158.  */
  159. if ($abort == FALSE) {
  160.     if (!isset($selected)) {
  161.         PMA_checkParameters(array('field'));
  162.         $selected[]   = $field;
  163.         $selected_cnt = 1;
  164.     } else { // from a multiple submit
  165.         $selected_cnt = count($selected);
  166.     }
  167.  
  168.     // TODO: optimize in case of multiple fields to modify
  169.     for ($i = 0; $i < $selected_cnt; $i++) {
  170.         if (!empty($submit_mult)) {
  171.             $field = PMA_sqlAddslashes(urldecode($selected[$i]), TRUE);
  172.         } else {
  173.             $field = PMA_sqlAddslashes($selected[$i], TRUE);
  174.         }
  175.         $result        = PMA_DBI_query('SHOW FULL FIELDS FROM ' . PMA_backquote($table) . ' FROM ' . PMA_backquote($db) . ' LIKE \'' . $field . '\';');
  176.         $fields_meta[] = PMA_DBI_fetch_assoc($result);
  177.         PMA_DBI_free_result($result);
  178.     }
  179.     $num_fields  = count($fields_meta);
  180.     $action      = 'tbl_alter.php';
  181.  
  182.     // Get more complete field information
  183.     // For now, this is done just for MySQL 4.1.2+ new TIMESTAMP options
  184.     // but later, if the analyser returns more information, it
  185.     // could be executed for any MySQL version and replace
  186.     // the info given by SHOW FULL FIELDS FROM.
  187.     // TODO: put this code into a require()
  188.     // or maybe make it part of PMA_DBI_get_fields();
  189.  
  190.     // We also need this to correctly learn if a TIMESTAMP is NOT NULL, since
  191.     // SHOW FULL FIELDS says NULL and SHOW CREATE TABLE says NOT NULL (tested 
  192.     // in MySQL 4.0.25).
  193.  
  194.     $show_create_table = PMA_DBI_fetch_value(
  195.         'SHOW CREATE TABLE ' . PMA_backquote($db) . '.' . PMA_backquote($table),
  196.         0, 1 );
  197.     $analyzed_sql = PMA_SQP_analyze( PMA_SQP_parse( $show_create_table ) );
  198.  
  199.     require('./libraries/tbl_properties.inc.php');
  200. }
  201.  
  202.  
  203. /**
  204.  * Displays the footer
  205.  */
  206. require_once('./libraries/footer.inc.php');
  207. ?>
  208.